package com.lsus.golkhadi.client.viewsImpl;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.lsus.golkhadi.client.ILoginService;
import com.lsus.golkhadi.client.ILoginServiceAsync;
import com.lsus.golkhadi.client.views.ILoginPanel;
import com.lsus.golkhadi.shared.Player;

public class LoginPanel extends Composite implements ILoginPanel {

	public static Player player = null;
	
	private static LoginPanelUiBinder uiBinder = GWT.create(LoginPanelUiBinder.class);

	interface LoginPanelUiBinder extends UiBinder<Widget, LoginPanel> {
	}
	
	final ILoginServiceAsync loginService = ( ILoginServiceAsync )GWT.create( ILoginService.class );
	
	@UiField TextBox username;
	@UiField PasswordTextBox password;
	@UiField Button login;
	
	public LoginPanel() {
		initWidget(uiBinder.createAndBindUi(this));
	}
	
	@UiHandler("login")
	void onClick(ClickEvent e){
		loginService.userIsValid(username.getValue(), password.getValue(), new AsyncCallback<Player>() {
			@Override
			public void onSuccess(Player result) {
				player = result;
				Window.alert(result.getSessionId() + result.getPosition());
			}

			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Sorry conneciton problem!");
			}
		});
	}
	
	

}
